﻿//​定义一个MyString类，该类的声明如下：
#define _CRT_SECURE_NO_WARNINGS1
#include<iostream>
#include<string.h>
using namespace std;
class MyString {
	char* ptr;
public:
	int size()const {
		return strlen(ptr);
	}
	MyString() {
		ptr = new char[1];
		ptr[0] = '\0';
	}
	MyString(const char* p) {
		ptr = new char[strlen(p) + 1];
		strcpy(ptr, p);
	}
	MyString(const MyString& str) {
		ptr = new char[str.size() + 1];
		strcpy(ptr, str.ptr);
	}
	~MyString() {
		delete[]ptr;
	}
	friend ostream& operator<<(ostream& out, const MyString& str);
	friend MyString operator+(const MyString& s1, const MyString& s2);
	friend int operator==(const MyString& s1, const MyString& s2);
	friend int operator!=(const MyString& s1, const MyString& s2);
	friend int operator<(const MyString& s1, const MyString& s2);
	friend int operator<=(const MyString& s1, const MyString& s2);
	friend int operator>(const MyString& s1, const MyString& s2);
	friend int operator>=(const MyString& s1, const MyString& s2);
	MyString& operator=(const MyString& s);
	MyString& operator+=(const MyString& s);
	char operator[](int i);
};
char MyString::operator[](int i)
{
	return ptr[i];
}
MyString& MyString::operator+=(const MyString& s)
{
	MyString temp;
	int len = size()+s.size();
	temp.ptr = new char[len + 1];
	strcpy(temp.ptr,ptr);
	strcat(temp.ptr,s.ptr);
	*this = temp;
	return *this;
}
MyString & MyString::operator=(const MyString& s)
{
	int len = s.size();
	ptr = new char[len + 1];
	strcpy(ptr, s.ptr);
	return *this;
}
int operator>=(const MyString& s1, const MyString& s2)
{
	int a = strcmp(s1.ptr, s2.ptr);
		if (a>= 0)
	{
			return 1;
	}
		else
		{
			return 0;
		}
}
int operator>(const MyString& s1, const MyString& s2)
{

	int a = strcmp(s1.ptr, s2.ptr);
	if (a > 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int operator<=(const MyString& s1, const MyString& s2) {
	int a = strcmp(s1.ptr, s2.ptr);
	if (a <=0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int operator<(const MyString& s1, const MyString& s2)
{
	int a = strcmp(s1.ptr, s2.ptr);
	if (a <0)
	{
		return 1;
	}
	else
	{
		return 0;
	}

}
int operator!=(const MyString& s1, const MyString& s2)
{
	int a = strcmp(s1.ptr, s2.ptr);
	if (a != 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int operator==(const MyString& s1, const MyString& s2)
{

	int a = strcmp(s1.ptr, s2.ptr);
	if (a == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
MyString operator+(const MyString& s1, const MyString& s2)
{
	MyString temp;
int len = s1.size() + s2.size();
	temp.ptr = new char[len + 1];
	strcpy(temp.ptr, s1.ptr);
	strcat(temp.ptr, s2.ptr);
	return temp;
}

ostream& operator<<(ostream& out, const MyString& str)
{
	int i = 0;
	for (i; i < str.size(); i++)
	{
		cout << str.ptr[i];
	}
	return out;
}
//对该类重载教材233页表6 - 1中列出的那些运算符（这些运算符的含义和string类一致）。
//
//
//main函数也写好了，请根据上面的类声明部分和main函数的内容，
// 将未实现的运算符函数进行实现。main函数如下：
//
int main() {

	MyString s1("Hello"), s2("World"), s3("China");
	cout << (s1 > s2) << " " << (s1 >= s2) << " " << (s1 <= s2) << " " << (s2 < s3) << endl;
	cout << (s1 == s2) << " " << (s1 != s3) << endl;
	MyString s4 = s1 + s2;
	cout << s4 << endl;
	s1 = s2 = s3;
	cout << s1 << " " << s2 << " " << s3 << endl;
	s4 += s3;
	cout << s4 << endl;
	for (int i = s4.size() - 1; i >= 0; i--)
		cout << s4[i];
	cout << endl;
	return 0;
}